package com.zsc.projectmanagementapi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsc.projectmanagementapi.dao.ProjectStateMapper;
import com.zsc.projectmanagementapi.pojo.Do.ProjectState;
import com.zsc.projectmanagementapi.service.ProjectStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LinHj
 * @date 2021/6/10 12:59
 **/
@Service
public class ProjectStateServiceImpl implements ProjectStateService {

    @Autowired
    private ProjectStateMapper projectStateMapper;
    @Autowired
    private  RedisTemplate redisTemplate;

    @Override
    public PageInfo<ProjectState> getAllProjectsState(int page,int count,String projectName,Integer imp,String sort) {
        PageHelper.startPage(page,count);
        List<ProjectState> projectStates = projectStateMapper.getAllProjectState(projectName,imp,sort);
        PageInfo<ProjectState> projectStatePageInfo = new PageInfo<>(projectStates);
        return projectStatePageInfo;
    }
    @Override
    public PageInfo<ProjectState> getAllProjectsStateSort(int page,int count,String sort) {
        PageHelper.startPage(page,count);
        List<ProjectState> projectStates = new ArrayList<>();
        switch (sort){
            case "+id":
                projectStates = projectStateMapper.getAllProjectStateAsc();
                break;
            case  "-id":
                projectStates = projectStateMapper.getAllProjectStateDesc();
            default:
                break;
        }
        PageInfo<ProjectState> projectStatePageInfo = new PageInfo<>(projectStates);
        return projectStatePageInfo;
    }

    @Override
    @Cacheable(value = "project_state",key = "'project_' + #id")
    public ProjectState getProjectStateById(String id) {
        QueryWrapper<ProjectState> projectStateQueryWrapper = new QueryWrapper<>();
        projectStateQueryWrapper.eq("project_id",id);
        ProjectState projectState = projectStateMapper.selectOne(projectStateQueryWrapper);
        return projectState;
    }

    @Override
    public int addProjectState(ProjectState projectState) {
        return projectStateMapper.insert(projectState);

    }

    @Override
    public int updateProjectState(ProjectState projectState) {
        QueryWrapper<ProjectState> projectStateQueryWrapper = new QueryWrapper<>();
        projectStateQueryWrapper.eq("project_id",projectState.getProjectId());
        int i = projectStateMapper.update(projectState,projectStateQueryWrapper);
        redisTemplate.delete("project_"+ projectState.getProjectId());
        return i;
    }

    @Override
    public int deleteProjectStateById(String id) {
        int i = projectStateMapper.deleteById(id);
        redisTemplate.delete("project_"+ id);
        return i;
    }
}
